package com.chj.jz.dp.class03;

public class Code05_BestTimetoBuyandSellStock {
	/**
	 * @param prices:
	 *            Given an integer array
	 * @return: Maximum profit
	 */
	public static int maxProfit(int[] prices) {
		// write your code here

		int n = prices.length;
		if (n == 0) {
			return 0;
		}

		int res = 0;
		int minV = prices[0];
		for (int i = 1; i < n; i++) {
			res = Math.max(res, prices[i] - minV);
			minV = Math.min(minV, prices[i]);
		}

		return res;
	}

	public static void main(String[] args) {
		{
			int[] arr = new int[] { 3, 2, 3, 1, 2 };
			System.out.println(maxProfit(arr));
		}
		{
			int[] arr = new int[] { 1, 2, 3, 4, 5 };
			System.out.println(maxProfit(arr));
		}
		{
			int[] arr = new int[] { 5, 4, 3, 2, 1 };
			System.out.println(maxProfit(arr));
		}

	}
}
